from django.shortcuts import render

# Create your views here.

from rest_framework.generics import ListAPIView
from rest_framework.pagination import PageNumberPagination
from rest_framework.filters import OrderingFilter

from sciencePopularization.models import SciencePopularizationDirection, PopularScienceArticle
from sciencePopularization.ser import SciencePopularizationDirectionSerializer, PopularScienceArticleSerializer

# 分页配置
class StandardResultsSetPagination(PageNumberPagination):
    page_size = 10
    page_size_query_param = 'page_size'
    max_page_size = 100

# 排序配置
class CustomOrderingFilter(OrderingFilter):
    ordering_params = ('name', 'publish_date')

# 科普文章方向 ListAPIView
class SciencePopularizationDirectionListView(ListAPIView):
    serializer_class = SciencePopularizationDirectionSerializer
    pagination_class = StandardResultsSetPagination
    filter_backends = [CustomOrderingFilter]
    ordering_fields = ['name']

    def get_queryset(self):
        return SciencePopularizationDirection.objects.all()

# 科普文章内容 ListAPIView
class PopularScienceArticleListView(ListAPIView):
    serializer_class = PopularScienceArticleSerializer
    pagination_class = StandardResultsSetPagination
    filter_backends = [CustomOrderingFilter]
    ordering_fields = ['publish_date', 'title']

    def get_queryset(self):
        return PopularScienceArticle.objects.all()
